<template>
  <div class="app-container zzjg">
    <el-row style="margin-bottom: 10px">
      <el-col :span="17" :xs="24">
        <el-input
          v-model="queryParams.questionTypeName"
          clearable
          placeholder="问题分类名称"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
        <el-select
          v-model="queryParams.isValid"
          clearable
          placeholder="请选择状态"
          style="margin-left: 20px"
        >
          <el-option
            v-for="item in isValidOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button
          icon="Search"
          style="margin-left: 15px"
          type="primary"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col
        :span="7"
        :xs="24"
        style="
          height: 38px;
          display: flex;
          align-items: center;
          justify-content: end;
        "
      >
        <el-button icon="Plus" plain type="primary" @click="handleAdd"
          >新建问题分类</el-button
        >
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      row-key="questionTypeId"
      stripe
    >
      <el-table-column
        label="问题分类名称"
        prop="questionTypeName"
      ></el-table-column>
      <el-table-column label="状态" prop="isValid">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <span
              :class="
                scope.row.isValid == 0 ? 'node-span-green' : 'node-span-red'
              "
            ></span>
            <span> {{ scope.row.isValid == 0 ? '有效' : '无效' }}</span>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="创建人" prop="createName"></el-table-column>
      <el-table-column label="创建时间" prop="createDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        label="操作"
        width="300"
      >
        <template #default="scope">
          <el-button
            icon="el-icon-edit"
            plain
            size="small"
            style="padding: 5px"
            type="primary"
            @click="handleCommand('handleUpdate', scope.row)"
            >修改</el-button
          >
          <el-button
            icon="el-icon-setting"
            plain
            size="small"
            style="padding: 5px"
            type="primary"
            @click="handleCommand('handleSetState', scope.row)"
            >设置状态</el-button
          >
          <!--          <el-button-->
          <!--            icon="el-icon-delete"-->
          <!--            plain-->
          <!--            size="small"-->
          <!--            style="padding: 5px"-->
          <!--            type="danger"-->
          <!--            @click="handleCommand('handleDelete', scope.row)"-->
          <!--            >删除</el-button-->
          <!--          >-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      v-model="open"
      :title="title"
      append-to-body
      destroy-on-close
      width="500px"
    >
      <el-form
        v-if="title != '设置状态'"
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="问题分类名称:" prop="questionTypeName">
          <el-input
            v-model="form.questionTypeName"
            clearable
            maxlength="50"
            placeholder="请输入问题分类名称"
          />
        </el-form-item>

        <el-form-item label="上级分类:" prop="parentId">
          <el-tree-select
            v-model="form.parentId"
            :data="tableData"
            :props="{
              value: 'questionTypeId',
              label: 'questionTypeName',
              children: 'children'
            }"
            check-strictly
            class="w-full"
            clearable
            filterable
            placeholder="选择上级分类"
            value-key="questionTypeId"
          />
        </el-form-item>
      </el-form>
      <el-form
        v-else
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="问题分类名称:">
          <el-input v-model="form.questionTypeName" disabled />
        </el-form-item>
        <el-form-item label="选择状态:">
          <el-radio-group v-model="form.isValid">
            <el-radio :label="1">无效</el-radio>
            <el-radio :label="0">有效</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button v-loading="loadingBtm" type="primary" @click="submitForm"
            >保 存</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="QuestionClassification" setup>
import { getCurrentInstance, reactive, toRefs } from 'vue'
import { ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import {
  deleteKnowledge,
  knowledgelList,
  knowledgeSave
} from '@/api/callCenter'
const { proxy } = getCurrentInstance()
const data = reactive({
  // 表格树数据
  tableData: [],
  // 遮罩层
  loading: true,
  // 选中数组
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 弹出层标题
  title: '',
  // 是否显示弹出层
  open: false,
  // 表单参数
  form: {},
  // 查询参数
  queryParams: {
    questionTypeName: undefined,
    isValid: undefined,
    pageNum: 1,
    pageSize: 9999
  },
  //状态
  isValidOption: [
    {
      label: '有效',
      value: '0'
    },
    {
      label: '无效',
      value: '1'
    }
  ],
  // 表单校验
  rules: {
    questionTypeName: [
      {
        required: true,
        message: '问题分类名称不能为空',
        trigger: ['blur', 'change']
      }
    ]
  }
})
const {
  queryParams,
  form,
  open,
  tableData,
  loading,
  single,
  multiple,
  showSearch,
  total,
  title,
  rules,
  isValidOption
} = toRefs(data)
const formRef = ref()
const loadingBtm = ref(false)
const router = useRouter()

/** 查询部门列表 */
function getList() {
  loading.value = true
  knowledgelList(queryParams.value).then(response => {
    tableData.value = response.data
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
}

// 表单重置
function reset() {
  form.value = {
    isValid: '0',
    questionTypeName: undefined,
    parentId: undefined
  }
  proxy.resetForm('form')
}

/** 搜索按钮操作 */
function handleQuery() {
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    isValid: undefined,
    questionTypeName: undefined,
    pageNum: 1,
    pageSize: 9999
  }
  handleQuery()
}

/** 新增按钮操作 */
async function handleAdd() {
  reset()
  open.value = true
  title.value = '新建问题分类'
}

// 更多操作触发
function handleCommand(command, row) {
  reset()
  switch (command) {
    case 'handleUpdate':
      form.value = JSON.parse(JSON.stringify(row))
      form.value.parentId = row.parentId == 0 ? undefined : row.parentId
      console.log(row, form.value, 'row--修改')
      open.value = true
      title.value = '修改问题分类'
      break
    case 'handleSetState':
      form.value = JSON.parse(JSON.stringify(row))
      console.log(row, form.value, 'row--设置')
      form.value.isValid = parseInt(row.isValid)
      open.value = true
      title.value = '设置状态'
      break
    case 'handleDelete':
      ElMessageBox.confirm('是否确定删除该数据？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function () {
          return deleteKnowledge(row.questionTypeId)
        })
        .then(res => {
          if (res.code == 200) {
            getList()
            proxy.$modal.msgSuccess('删除成功')
          }
        })
        .catch(() => {})
      break
    default:
      break
  }
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['formRef'].validate(valid => {
    if (valid) {
      loadingBtm.value = true
      knowledgeSave(form.value)
        .then(res => {
          proxy.$modal.msgSuccess('操作成功')
          handleQuery()
          open.value = false
        })
        .finally(e => {
          loadingBtm.value = false
        })
    }
  })
}

/** 删除按钮操作 */

getList()
</script>

<style lang="scss" scoped>
.expanded.el-tree-node__expand-icon.none {
  display: none;
}

.node-span-red {
  font-size: 14px;
  color: red;

  &::before {
    content: '';
    display: block;
    width: 5px;
    height: 5px;
    background: red;
    border-radius: 50%;
    margin-right: 6px;
  }
}

.node-span-green {
  font-size: 14px;
  color: green;

  &::before {
    content: '';
    display: block;
    width: 5px;
    height: 5px;
    background: green;
    border-radius: 50%;
    margin-right: 6px;
  }
}
</style>
